--[[ Lambda Chat
     Part of LambdaUI made by Gaius
	 Graphics are made by Gaius
	 ------------------------------
	 Contact me in irc://phcnet.de/#wow-interface
--]]

local filePath = "Interface\\AddOns\\LambdaUI\\"
local genericFilePath = "Interface\\AddOns\\LambdaUI\\Generic\\"

--[[  Lambda Chat Class

--]]
local Lambda = {}

function Lambda:new(ChannelId)
	local Lambda = {}
    local filePath = filePath.."Chat\\"
	
	Lambda = getglobal("ChatFrame"..ChannelId)

	if Lambda then
	
		local children = {Lambda:GetChildren()}
		for i,v in pairs(children) do
			v:SetAlpha(0)
		end
		
		Lambda:SetFont(genericFilePath.."normal.ttf",10)
		
		Lambda.BorderTexture = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Border","BORDER")
		Lambda.BorderTexture1 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Border1","BORDER")
		Lambda.BorderTexture2 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Border2","BORDER")
		Lambda.BorderTexture3 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Border3","BORDER")
		Lambda.BorderTexture4 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Border4","BORDER")
		Lambda.BorderTexture5 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Border5","BORDER")
		
		Lambda.BackgroundTexture = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Background","BACKGROUND")
		Lambda.BackgroundTexture1 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Background1","BACKGROUND")
		Lambda.BackgroundTexture2 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Background2","BACKGROUND")
		Lambda.BackgroundTexture3 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Background3","BACKGROUND")
		Lambda.BackgroundTexture4 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Background4","BACKGROUND")
		Lambda.BackgroundTexture5 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Background5","BACKGROUND")
		Lambda.BackgroundTexture6 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Background6","BACKGROUND")
		Lambda.BackgroundTexture7 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Background7","BACKGROUND")
		Lambda.BackgroundTexture8 = Lambda:CreateTexture("LambdaChatFrame"..ChannelId.."Background8","BACKGROUND")
		Lambda.Voice = CreateFrame("Button","LambdaChatFrame"..ChannelId.."Voice", Lambda)
		Lambda.AllDown = CreateFrame("Button","LambdaChatFrame"..ChannelId.."AllDown", Lambda)
		
		ChatFrameMenuButton:Hide()
		ChatFrameMenuButton.Show = function() end
		
		Lambda:EnableMouseWheel(1)
		
		Lambda:SetScript("OnMouseWheel", function() 
			if arg1 > 0 then
				Lambda:ScrollUp()
			else
				Lambda:ScrollDown()
			end
		end)
	
		Lambda:SetFrameLevel(4)
		
		Lambda.OnUpdate = function()
			if select(1,Lambda:GetMinResize()) ~= 294 then -- must be the first time
				Lambda:SetMinResize(294, 100)
				Lambda:SetScript("OnUpdate",nil)
				
			end
		end
		
		Lambda:SetScript("OnUpdate",Lambda.OnUpdate)
		
		Lambda.BackgroundTexture:SetTexture(filePath.."BackgroundCenter.tga")
		Lambda.BackgroundTexture:SetPoint("TOPLEFT",Lambda,"TOPLEFT",32, -28)
		Lambda.BackgroundTexture:SetPoint("BOTTOMRIGHT",Lambda,"BOTTOMRIGHT",-18, 20)
		
		Lambda.BackgroundTexture1:SetTexture(filePath.."BackgroundTopLeft.tga")
		Lambda.BackgroundTexture1:SetPoint("BOTTOMRIGHT",Lambda,"TOPLEFT",32, -28)
		Lambda.BackgroundTexture1:SetWidth(32)
		Lambda.BackgroundTexture1:SetHeight(32)
		
		Lambda.BackgroundTexture2:SetTexture(filePath.."BackgroundTop.tga")
		Lambda.BackgroundTexture2:SetPoint("BOTTOMLEFT",Lambda,"TOPLEFT",32, -28)
		Lambda.BackgroundTexture2:SetPoint("BOTTOMRIGHT",Lambda,"TOPRIGHT",-18, -28)
		Lambda.BackgroundTexture2:SetHeight(32)
		
		Lambda.BackgroundTexture3:SetTexture(filePath.."BackgroundTopRight.tga")
		Lambda.BackgroundTexture3:SetPoint("BOTTOMLEFT",Lambda,"TOPRIGHT",-18, -28)
		Lambda.BackgroundTexture3:SetWidth(32)
		Lambda.BackgroundTexture3:SetHeight(32)
		
		Lambda.BackgroundTexture4:SetTexture(filePath.."BackgroundLeft.tga")
		Lambda.BackgroundTexture4:SetPoint("TOPLEFT",Lambda,"TOPLEFT",0, -28)
		Lambda.BackgroundTexture4:SetPoint("BOTTOMLEFT",Lambda,"BOTTOMLEFT",0, 20)
		Lambda.BackgroundTexture4:SetWidth(32)
		
		Lambda.BackgroundTexture5:SetTexture(filePath.."BackgroundRight.tga")
		Lambda.BackgroundTexture5:SetPoint("TOPRIGHT",Lambda,"TOPRIGHT",14, -28)
		Lambda.BackgroundTexture5:SetPoint("BOTTOMRIGHT",Lambda,"BOTTOMRIGHT",14, 20)
		Lambda.BackgroundTexture5:SetWidth(32)
		
		Lambda.BackgroundTexture6:SetTexture(filePath.."BackgroundBottomLeft.tga")
		Lambda.BackgroundTexture6:SetPoint("BOTTOMRIGHT",Lambda,"BOTTOMLEFT",32, -12)
		Lambda.BackgroundTexture6:SetWidth(32)
		Lambda.BackgroundTexture6:SetHeight(32)
		
		Lambda.BackgroundTexture7:SetTexture(filePath.."BackgroundBottom.tga")
		Lambda.BackgroundTexture7:SetPoint("BOTTOMLEFT",Lambda,"BOTTOMLEFT",32, -12)
		Lambda.BackgroundTexture7:SetPoint("BOTTOMRIGHT",Lambda,"BOTTOMRIGHT",-14, -12)
		Lambda.BackgroundTexture7:SetHeight(32)
		
		Lambda.BackgroundTexture8:SetTexture(filePath.."BackgroundBottomRight.tga")
		Lambda.BackgroundTexture8:SetPoint("BOTTOMLEFT",Lambda,"BOTTOMRIGHT",-14, -12)
		Lambda.BackgroundTexture8:SetWidth(32)
		Lambda.BackgroundTexture8:SetHeight(32)
		
		Lambda.BorderTexture:SetTexture(filePath.."BorderTop.tga")
		
		Lambda.BorderTexture1:SetTexture(filePath.."BorderTop.tga")
		Lambda.BorderTexture1:SetPoint("TOPLEFT",Lambda,"TOPRIGHT",4, 4)
		Lambda.BorderTexture1:SetWidth(32)
		Lambda.BorderTexture1:SetHeight(128)
		
		Lambda.BorderTexture2:SetTexture(filePath.."BorderMiddle.tga")
		Lambda.BorderTexture2:SetPoint("TOPLEFT",Lambda,"TOPRIGHT",4, -49)
		Lambda.BorderTexture2:SetPoint("BOTTOMLEFT",Lambda,"BOTTOMRIGHT",4, 0)
		Lambda.BorderTexture2:SetWidth(32)
	
		Lambda.BorderTexture3:SetTexture(filePath.."BorderBottomRight.tga")
		Lambda.BorderTexture3:SetPoint("BOTTOMLEFT",Lambda,"BOTTOMRIGHT",-15, -21)
		Lambda.BorderTexture3:SetWidth(32)
		Lambda.BorderTexture3:SetHeight(32)
		
		Lambda.BorderTexture4:SetTexture(filePath.."BorderBottom.tga")
		Lambda.BorderTexture4:SetPoint("BOTTOMLEFT",Lambda,"BOTTOMLEFT",28, -21)
		Lambda.BorderTexture4:SetPoint("BOTTOMRIGHT",Lambda,"BOTTOMRIGHT",-4, -9)
		Lambda.BorderTexture4:SetHeight(32)
		
		Lambda.BorderTexture5:SetTexture(filePath.."BorderBottomLeft.tga")
		Lambda.BorderTexture5:SetPoint("BOTTOMLEFT",Lambda,"BOTTOMLEFT",-4, -21)
		Lambda.BorderTexture5:SetHeight(32)
		Lambda.BorderTexture5:SetWidth(32)
		
		Lambda.BorderTexture.SetVertexColor = function(r,g,b,a)
				Lambda.BorderTexture1:SetVertexColor(unpack(LambdaUI.ThemeSettings.Border))
				Lambda.BorderTexture2:SetVertexColor(unpack(LambdaUI.ThemeSettings.Border))
				Lambda.BorderTexture3:SetVertexColor(unpack(LambdaUI.ThemeSettings.Border))
				Lambda.BorderTexture4:SetVertexColor(unpack(LambdaUI.ThemeSettings.Border))
				Lambda.BorderTexture5:SetVertexColor(unpack(LambdaUI.ThemeSettings.Border))
		end
		
		Lambda.Voice:SetNormalTexture(filePath.."Voice.tga")
		Lambda.Voice:SetScript("OnClick", function()
			ChatMenu:Show()
		end)
		
		Lambda.Voice:SetPoint("TOPLEFT",Lambda,"TOPRIGHT",5,1)
		Lambda.Voice:SetWidth(32)
		Lambda.Voice:SetHeight(32)
		Lambda.Voice:SetFrameLevel(0)
		
		Lambda.AllDown:SetNormalTexture(filePath.."AllDown.tga")
		Lambda.AllDown:SetScript("OnClick", function()
			Lambda:ScrollToBottom()
		end)
		
		Lambda:SetFrameLevel(1)
		
		Lambda.AllDown:SetPoint("TOPLEFT",Lambda,"BOTTOMRIGHT", -2, -2)
		Lambda.AllDown:SetWidth(16)
		Lambda.AllDown:SetHeight(16)
		Lambda.AllDown:SetFrameLevel(0)
		
		Lambda.BorderTexture:SetVertexColor(unpack(LambdaUI.ThemeSettings.Border))
				
		table.insert(LambdaObjectTable,Lambda:GetName())
		
		local bg = getglobal("ChatFrame"..ChannelId.."Background")
		bg:SetAlpha(0)
		
		-- preload bg color
		local r,g,b = bg:GetVertexColor()
		
		Lambda.BackgroundTexture:SetVertexColor(r,g,b,1)
		Lambda.BackgroundTexture1:SetVertexColor(r,g,b,1)
		Lambda.BackgroundTexture2:SetVertexColor(r,g,b,1)
		Lambda.BackgroundTexture3:SetVertexColor(r,g,b,1)
		Lambda.BackgroundTexture4:SetVertexColor(r,g,b,1)
		Lambda.BackgroundTexture5:SetVertexColor(r,g,b,1)
		Lambda.BackgroundTexture6:SetVertexColor(r,g,b,1)
		Lambda.BackgroundTexture7:SetVertexColor(r,g,b,1)
		Lambda.BackgroundTexture8:SetVertexColor(r,g,b,1)
		
		-- move forward
		
		if bg then
			bg.SetAlpha = function(a)
				if tonumber(a) then
					Lambda.BackgroundTexture:SetAlpha(a)
				else
					Lambda.BackgroundTexture:SetAlpha(1)
				end
			end
			bg.SetVertexColor = function(a, r, g, b)
				Lambda.BackgroundTexture:SetVertexColor(r,g,b,a)
				Lambda.BackgroundTexture1:SetVertexColor(r,g,b,a)
				Lambda.BackgroundTexture2:SetVertexColor(r,g,b,a)
				Lambda.BackgroundTexture3:SetVertexColor(r,g,b,a)
				Lambda.BackgroundTexture4:SetVertexColor(r,g,b,a)
				Lambda.BackgroundTexture5:SetVertexColor(r,g,b,a)
				Lambda.BackgroundTexture6:SetVertexColor(r,g,b,a)
				Lambda.BackgroundTexture7:SetVertexColor(r,g,b,a)
				Lambda.BackgroundTexture8:SetVertexColor(r,g,b,a)
			end
		end
	end
		return Lambda
end 

local LambdaInit = CreateFrame("Frame") -- workaround for layout-cache.txt

LambdaInit:SetScript("OnUpdate", function()
	for i=1,9 do
	LambdaChat = Lambda:new(i)
	if LambdaChat then
		LambdaInit:SetScript("OnUpdate", function() end)
	end
	end
end)
